package com.soul.soulcommon.utils;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.JWTVerifier;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.Date;

@Component
@Slf4j
public class JwtUtils {
    private static final long EXPIRE_TIME = 1000 * 60 * 60 * 24 * 3;

    // 密钥
    private static final String SECRET = "your-secret-key-here-must-be-longer-than-256-bits-for-security";

    /**
     * 生成JWT Token
     */
    public String generateToken(Integer id, String userName, String roleName) {
        try {
            Algorithm algorithm = Algorithm.HMAC256(SECRET);
            return JWT.create()
                    .withClaim("id", id)
                    .withClaim("userName", userName)
                    .withClaim("roleName", roleName)
                    .withIssuedAt(new Date())
                    .withExpiresAt(new Date(System.currentTimeMillis() + EXPIRE_TIME))
                    .sign(algorithm);
        } catch (Exception e) {
            log.error("生成Token失败: {}", e.getMessage());
            return null;
        }
    }

    public static void main(String[] args) {
        JwtUtils jwtUtils = new JwtUtils();
        String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6OCwidXNlck5hbWUiOiIw5Y-35p2O6L-Q5aSpIiwicm9sZU5hbWUiOiJzdHJpbmciLCJpYXQiOjE3MzA3MDAyNDAsImV4cCI6MTczMDk1OTQ0MH0.-S-L7Dk-qf5SIYKSSy-Nauhw5jgXgTldqpLcqVt-owg";
        System.out.println("id:"+jwtUtils.getUserIdFromToken(token)+"\n"+
        "userName:"+jwtUtils.getUserNameFromToken(token)+"\n"+
        "roleName" + jwtUtils.getRoleNameFromToken(token)+"\n"+
                jwtUtils.validateToken(token));
    }
    /**
     * 验证Token
     */
    public boolean validateToken(String token) {
        try {
            Algorithm algorithm = Algorithm.HMAC256(SECRET);
            JWTVerifier verifier = JWT.require(algorithm).build();
            verifier.verify(token);
            return true;
        } catch (Exception e) {
            log.error("Token验证失败: {}", e.getMessage());
            return false;
        }
    }

    /**
     * 从Token中获取用户ID
     */
    public Integer getUserIdFromToken(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("id").asInt();
        } catch (Exception e) {
            log.error("获取用户ID失败: {}", e.getMessage());
            return null;
        }
    }

    /**
     * 从Token中获取用户名
     */
    public String getUserNameFromToken(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("userName").asString();
        } catch (Exception e) {
            log.error("获取用户名失败: {}", e.getMessage());
            return null;
        }
    }

    /**
     * 从Token中获取角色
     */
    public String getRoleNameFromToken(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("roleName").asString();
        } catch (Exception e) {
            log.error("获取角色失败: {}", e.getMessage());
            return null;
        }
    }

    /**
     * 检查Token是否过期
     */
    public boolean isTokenExpired(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getExpiresAt().before(new Date());
        } catch (Exception e) {
            log.error("检查Token过期失败: {}", e.getMessage());
            return true;
        }
    }
}
